package cn.edu.scau.cmi.wujiaxian.comprehensive.service;

import java.util.ArrayList;

import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.ClazzDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.CourseDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.dao.TeacherDao;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Clazz;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Course;
import cn.edu.scau.cmi.wujiaxian.comprehensive.model.Teacher;

public class CourseService {
	
	public static void main(String[] args) {
		CourseService courseService = new CourseService();
//		courseService.addCourse("Java程序设计", "SX001", "肖磊", "2017级软件工程1班");
//		courseService.deleteCourse("Java程序设计", "2017级软件工程2班");
		System.out.println(courseService.selectCourses(2));
	}
	
	public ArrayList<Course> selectCourses(Integer teacherId) {
		CourseDao courseDao = new CourseDao();
		ArrayList<Course> courses = courseDao.select("teacherId", teacherId);
		
		
		
		return courses;
	}
	
	/**
	 * 增加一个课程
	 * @param name
	 * @param courseNumber
	 * @param teacherName
	 * @param graceAndMajorAndClazz
	 */
	public void addCourse(String name, String courseNumber, String teacherName, String graceAndMajorAndClazz) {
		TeacherDao teacherDao = new TeacherDao();
		ArrayList<Teacher> teachers = teacherDao.select("name", teacherName);
		ClazzDao clazzDao = new ClazzDao();
		ArrayList<Clazz> clazzs = clazzDao.select("graceAndMajorAndClazz", graceAndMajorAndClazz);
		Course course = new Course();
		course.setName(name);
		course.setCourseNumber(courseNumber);
		course.setTeacherId(teachers.get(0).getId());
		course.setClazzId(clazzs.get(0).getId());
		CourseDao courseDao = new CourseDao();
		courseDao.add(course);
	}
	
	/**
	 * 删除一个课程
	 * @param name
	 * @param graceAndMajorAndClazz
	 */
	public void deleteCourse(String name, String graceAndMajorAndClazz) {
		ClazzDao clazzDao = new ClazzDao();
		ArrayList<Clazz> clazzs = clazzDao.select("graceAndMajorAndClazz", graceAndMajorAndClazz);
		CourseDao courseDao = new CourseDao();
		ArrayList<Course> courses = courseDao.select("clazzId", clazzs.get(0).getId());
		for (Course course : courses) {
			if (course.getName().equals(name)) {
				courseDao.delete("id", course.getId());
				break;
			}
		}
	}
	
}
